Comments

In the previous lecture we learnt how to print "hello world". Lets try to do that again. Click on the cell below and hit the 'run' button. Go on, I'll wait for you...


In [2]:
# print("Hello World)

Woah !? Nothing happened!? Why is that?

The reason “hello world” did not get printed to the console in this case is because that line of code is “commented out”. Comments in Python have a very simple syntax:

# {Any text we want}
# {More gibberish}

To use comments it is actually super easy, we just type the "#" character, and this will make Python ignore everything on that line from that point on. If we want comments to span multiple lines, then we have to remember to put a # at the start of every line. For example:


In [3]:
# Lots...
# and lots...
# of comments...

You can also place comments after some code, in which case the code executes. Here, let me show you:


In [4]:
print("this works")  # this works because the "#" symbol is placed AFTER the bit of code we want to run!


this works

What are comments for?

Comments have a few uses, but the main one is to communicate with other human beings (including your future self!). Helpful comments can make reading and understanding code a lot less difficult, and that's why leaving good comments is a GREAT idea.

Here, let me show you:


In [7]:
"abc" * 4  # ???


Out[7]:
'abcabcabcabc'

In [8]:
"a" * 3 # string * number repeats the character. Thus "a" * 2 = "aa" and "az" * 2 = "azaz".


Out[8]:
'aaa'

In later lectures I'll explain how multiplying strings work. But for now just notice that in the first case you didn't know what was going on (because there were no helpful comments), but you understand what is happening in the second case because the comment explains the code.

Also as a quick heads up, generally speaking, if you feel you need to write comments explaining the code then this often a sign that your code isn't actually good code. Good code usually has the property that you can just tell what it is doing just by looking at it. For example:


In [17]:
print( 4 * 2 ) # Very simply and clear code, you can tell what it does just by looking at it.
print( int(chr(52)).__mul__(int(chr(50))) ) # A TERRIBLE and confusing way to calculate "4 * 2"


8
8

Complex code often requires comments to explain what it does, and that's why comments are (sort-of) bad.

Make no mistake, comments are really useful and you SHOULD use them. However, it’s often a good idea to think of comments as a last-resort; only use them if you cannot think of a way to make your code less complicated. In later lectures, I'll discuss a number of techniques for writing simple code.

Homework

Your task for this week is to get the code in the box below to work, use your understanding of comments to fix it!


In [3]:
# print("\n            *      ,MMM8&&&.            *\n                  MMMM88&&&&&    .\n                 MMMM88&&&&&&&\n     *           MMM88&&&&&&&&\n                 MMM88&&&&&&&&\n                 'MMM88&&&&&&'\n                   'MMM8&&&'      *\n          |\\___/|\n          )     (             .              '\n         =\\     /=\n           )===(       *\n          /     \\\n          |     |\n         /       \\\n         \\       /\n  _/\\_/\\_/\\__  _/_/\\_/\\_/\\_/\\_/\\_/\\_/\\_/\\_/\\_\n  |  |  |  |( (  |  |  |  |  |  |  |  |  |  |\n  |  |  |  | ) ) |  |  |  |  |  |  |  |  |  |\n  |  |  |  |(_(  |  |  |  |  |  |  |  |  |  |\n  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |\n  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |\n")